/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.recharge.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.bi.recharge.dao.BIRechargeDao;
import com.boxin.ims.modules.recharge.entity.Recharge;
import com.boxin.ims.modules.recharge.entity.RechargeLog;
import com.boxin.ims.modules.recharge.service.RechargeLogService;
import com.boxin.ims.modules.recharge.service.RechargeService;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.WechatMemberService;

/**
 * 会员充值配置日志Controller
 * @author Jakemanse
 * @version 2014-03-03
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/recharge/rechargeLog")
public class RechargeLogController extends BaseController {
	
	@Autowired
	private RechargeService rechargeService;

	@Autowired
	private RechargeLogService rechargeLogService;
	
	@Autowired
	private WechatMemberService wechatMemberService;
	
	@Autowired
	private BIRechargeDao biRechargeDao;
	
	@ModelAttribute
	public RechargeLog get(@RequestParam(required=false) Long id) {
		if (id != null){
			return rechargeLogService.get(id);
		}else{
			return new RechargeLog();
		}
	}
	
	@RequiresPermissions("recharge:rechargeLog:view")
	@RequestMapping(value = {"list", ""})
	public String list(RechargeLog rechargeLog, HttpServletRequest request, HttpServletResponse response, Model model) throws ParseException {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			rechargeLog.setUser(user);
		}
		
		String startTime=request.getParameter("startTime");
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		 if(StringUtils.isNotEmpty(startTime)){
	        	
	        	String day=startTime.replace("-", "");
	        	
	        	rechargeLog.setDay(day);
	        }else{
	        	 startTime = formatter.format(new Date());
	        }
		
		 System.out.println(rechargeLog.getDay());
        Page<RechargeLog> page = rechargeLogService.find(new Page<RechargeLog>(request, response), rechargeLog); 
        model.addAttribute("page", page);
       
        
        Date startDate=formatter.parse(startTime);
        model.addAttribute("startTime", startDate);
		return "modules/recharge/rechargeLogList";
	}

	@RequiresPermissions("recharge:rechargeLog:view")
	@RequestMapping(value = "form")
	public String form(RechargeLog rechargeLog, Model model) {
	
		User user = UserUtils.getUser();
		if (rechargeLog.getUser() == null || rechargeLog.getUser().getId() <1){
			rechargeLog.setUser(user);
		}
		
		model.addAttribute("rechargeLog", rechargeLog);
		return "modules/recharge/rechargeLogForm";
	}

	@RequiresPermissions("recharge:rechargeLog:edit")
	@RequestMapping(value = "save")
	public String save(RechargeLog rechargeLog, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, rechargeLog)){
			return form(rechargeLog, model);
		}
		rechargeLogService.save(rechargeLog);
		addMessage(redirectAttributes, "保存会员充值配置日志'" + rechargeLog.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/recharge/rechargeLog/?repage";
	}
	
	@RequiresPermissions("recharge:rechargeLog:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		rechargeLogService.delete(id);
		addMessage(redirectAttributes, "删除会员充值配置日志成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/recharge/rechargeLog/?repage";
	}
	
	
	@RequiresPermissions("recharge:rechargeLog:view")
	@RequestMapping(value = "chackRechargeMoney")
	public String chackRechargeMoney(RechargeLog rechargeLog, Model model) {
	
		User user = UserUtils.getUser();
		if (rechargeLog.getUser() == null || rechargeLog.getUser().getId() <1){
			rechargeLog.setUser(user);
		}
		
		List<Map<String, Object>>  list=biRechargeDao.queryRechargeBymoney();
		
		model.addAttribute("list", list);
		return "modules/recharge/chackRechargeMoneyList";
	}
	
	@RequestMapping(value = "queryRechargeLogSumBynumber")
	public void queryRechargeLogSumBynumber(RechargeLog rechargeLog, Model model,HttpServletRequest request, HttpServletResponse response) {
	
		String number=request.getParameter("number");
		
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("number", number);
		List<Map<String, Object>>  list=biRechargeDao.queryRechargeLogSumBynumber(params);
		System.out.println(list.toString());
		
		writeJson(request, response, list);
		
	}
	
	@RequestMapping(value = "syncMoneyForm")
	public String syncMoneyForm( Model model,HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
	
		String number=request.getParameter("number");
		
		String recharge_money=request.getParameter("recharge_money");
		String member_money=request.getParameter("member_money");
		
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("number", number);
		List<Map<String, Object>>  list=biRechargeDao.queryRechargeLogSumBynumber(params);
		
		
		if(list.get(0).get("money")!=null&&StringUtils.isNotEmpty(list.get(0).get("money").toString())){
			model.addAttribute("money",list.get(0).get("money"));
		}else{
			model.addAttribute("money","数据库查不到该数据");
		}
		model.addAttribute("number", number);
		model.addAttribute("recharge_money", recharge_money);
		model.addAttribute("member_money", member_money);
		
		return "modules/recharge/chackRechargeMoneyForm";
		
	}
	
	
	@RequestMapping(value = "syncMoney")
	public String syncMoney( Model model,HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
	
		String number=request.getParameter("number");
		
		String recharge_money=request.getParameter("recharge_money");
		String member_money=request.getParameter("member_money");
		
		
			Recharge recharge =rechargeService.getRechargeByNumber(number);
			recharge.setMoney(Integer.parseInt(recharge_money));
			rechargeService.save(recharge);
			
			WechatMember  wechatMember=wechatMemberService.getByNumber(number);
			wechatMember.setMoney(Integer.parseInt(member_money));
			wechatMemberService.save(wechatMember);
			
		addMessage(redirectAttributes, "修改成功");
		return "redirect:"+Global.ADMIN_PATH+"/recharge/rechargeLog/chackRechargeMoney?repage";
		
	}
	

}
